"""
爬出豆瓣TOP250的电影票房
xgt-python
datetime : 2021/10/26
"""

# 导入相应的模块
import random
import re
import time
import requests


pattern= re.compile(r'<a[^>]*?>\s*<span class="title">(.*?)</span>')

# for循环拿到页面内容
for page in range(10):
    resp = requests.get(
        url=f'https://movie.douban.com/top250?start={page * 25}',
        headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' \
                                        'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;'
                      'q=0.9,image/webp,image/apng,*/*;'
                      'q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        },
    )

    items = pattern.findall(resp.text)
    for item in items:
        print(item)
    time.sleep(random.randint(1, 3))